<?php

namespace App\Services\Init;

use App\Constants\CodeData;
use App\Model\AppUpgrade;
use App\Utils\LoggerUtil;

class CheckService
{

    /**
     * Note - 客户端检查最新版本
     * DateTime 2024/8/13 11:21
     * User Admin
     */
    public function checkVersion($params)
    {
        $package_name = trim($params['package']);
        $from = $params['from'] ?? 1;
        $version_code = $params['version_code'] ?? 0;
        $channel = $params['channel'] ?? '';
        // 1精简包(采取动态加载so的版本),0完整包
        $so_patch_failed2force_upgrade = isset($params['so_patch_failed2force_upgrade']) ? intval($params['so_patch_failed2force_upgrade']) : 0;
        if (empty($package_name)) {
            return formatData(['reason' => 'no upgrade1'], 200, CodeData::SUCCESS, '没有版本更新');
        }
        $ret = AppUpgrade::query()
            ->select(['id', 'version_code', 'version_name', 'update_time', 'content', 'down_url', 'channel', 'focus'])
            ->where('type', $from)
            ->where('package_name', $package_name)
            ->where('status', 1)
            ->where(function ($query) use ($version_code) {
                $query->where('upgrade_version', "")->orWhere('upgrade_version', 'like', '%' . $version_code . '%');
            })->get()->toArray();
        if (!empty($ret)) {
            foreach ($ret as $v) {
                if (!empty($v['channel'])) {
                    $cache['channels'][$v['channel']] = $v; //一个包细分了多个渠道
                } else {
                    $cache['self'] = $v; //一个包代表所有，不细分
                }
            }
        }
        if (!empty($cache)) {
            $res = array(); //找不到记录
            if (!empty($cache['channels'][$channel])) {
                if ($so_patch_failed2force_upgrade == 1) {
                    // 精简包升级要特殊处理
                    if ($cache['channels'][$channel]['version_code'] >= $version_code) {
                        $res = $cache['channels'][$channel];
                    }
                } else {
                    if ($cache['channels'][$channel]['version_code'] > $version_code) {
                        $res = $cache['channels'][$channel];
                    }
                }
            } elseif (!empty($cache['self'])) {
                if ($so_patch_failed2force_upgrade == 1) {
                    // 精简包升级要特殊处理
                    if ($cache['self']['version_code'] >= $version_code) {
                        $res = $cache['self'];
                    }
                } else {
                    if ($cache['self']['version_code'] > $version_code) {
                        $res = $cache['self'];
                    }
                }
            }
            if (!empty($res)) {
                $retval = array(
                    'new_version_code' => $res['version_code'],
                    'new_version' => $res['version_name'],
                    'update_time' => $res['update_time'],
                    'update_content' => $res['content'],
                    'is_force' => $res['focus'], //0表示不强制更新，1表示强制更新
                    'down_url' => $res['down_url']
                );
                $logs = array_merge($params, $retval);
                LoggerUtil::write("app_upgrade_logs", $logs, "版本升级记录");
                return formatData($retval, 200, CodeData::NEED_TIPS);
            } else {
                return formatData(['reason' => 'no upgrade2'], 200, CodeData::SUCCESS, '没有版本更新');
            }
        } else {
            return formatData(['reason' => 'no upgrade3'], 200, CodeData::SUCCESS, '没有版本更新');
        }


    }
}